﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Assessment.Management.SharedComponent.Params.ManagerParam;
using Assessment.Management.SharedComponent.Constants;
using Assessment.Management.SharedComponent.Entities;

namespace Assessment.Management.DAO.CommonManagers
{
    public class IPDAO : BaseDaos
    {
        public IPDAO()
            : base()
        {
        }

        public void CheckIP(IPParam param)
        {

            string query = "Select * from IPAdress where IPAddress = @IPAddress";

            var cmd = base.GetSqlCommand();
            cmd.CommandText = query;
            cmd.Parameters.AddWithValue("@IPAddress", param.IPAddres.IPAddress);
            try
            {
                param.IPAddres = base.ExecuteQueryGetObject<IPAddres>(cmd);
            }
            finally
            {
                base.CloseConnection(cmd);
            }
        }
        public void CheckIDSubject(IPParam param)
        {

            string query = "Select * from IPAdress where SubjectID = @SubjectID";

            var cmd = base.GetSqlCommand();
            cmd.CommandText = query;
            cmd.Parameters.AddWithValue("@SubjectID", param.IPAddres.ConfigID);
            try
            {
                param.IPAddres = base.ExecuteQueryGetObject<IPAddres>(cmd);
            }
            finally
            {
                base.CloseConnection(cmd);
            }
        }
        public void CreateIP(IPParam param)
        {
            string query = @"Insert into IPAdress (IPAddress,SubjectID) 
                                        values(@IPAddress, @SubjectID)";
            //TODO: What the fuck is going on here?
            var cmd = base.GetSqlCommand();
            cmd.CommandText = query;
            cmd.Parameters.AddWithValue("@SubjectID", param.IPAddres.ConfigID);
            cmd.Parameters.AddWithValue("@IPAddress", param.IPAddres.IPAddress);
            try
            {
                int count = base.ExecuteNonQuery(cmd);
            }
            finally
            {
                base.CloseConnection(cmd);
            }
        }

        public void DeleteIPByConfigID(IPParam param)
        {
            string query = @"Delete from IPAdress Where ConfigID =@ConfigID";

            var cmd = base.GetSqlCommand();
            cmd.CommandText = query;
            cmd.Parameters.AddWithValue("@ConfigID", param.IPAddres.ConfigID);
            try
            {
                int count = base.ExecuteNonQuery(cmd);
            }
            finally
            {
                base.CloseConnection(cmd);
            }
        }

    }
}
